package com.douyang.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.douyang.base.model.Result;
import com.douyang.service.VideoUploadCacheService;
import com.douyang.base.expcetion.VideoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoma
 * @since 2025-02-28
 */
@RestController
@RequestMapping("/video/videoUploadCache")
public class VideoUploadCacheController {
    @Autowired
    private VideoUploadCacheService videoUploadCacheService;

    @GetMapping("/checkChunkExist")
    public Result<Boolean> checkChunkExist(String chunkMd5,String fileMd5){
        if(StringUtils.isEmpty(chunkMd5) || StringUtils.isEmpty(fileMd5)){
            throw new VideoException("checkChunkExist:空参警告");
        }
        return Result.success(videoUploadCacheService.checkChunkExist(chunkMd5,fileMd5));
    }

    @PostMapping("/uploadChunk")
    public Result<String> uploadChunk(@RequestParam("chunk") MultipartFile chunk,String chunkMd5,String fileMd5){
        return Result.success(videoUploadCacheService.uploadChunk(chunk,chunkMd5,fileMd5));
    }

    @PostMapping("/mergeChunk")
    public Result<String> mergeChunk(String fileMd5){
        if(StringUtils.isEmpty(fileMd5)){throw new VideoException("mergeChunk:参数异常");}
        videoUploadCacheService.mergeChunk(fileMd5);
        return Result.success("ok");
    }
}
